package com.qingluan.filecachetest;

import org.apache.http.Header;
import org.apache.http.HttpResponse;

import android.util.Log;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.BinaryHttpResponseHandler;
import com.loopj.android.http.RequestParams;

public class ImageDownloader {

	private static final String TAG = ImageDownloader.class.getName();
	
	public static  void Download(String url ,final DealByteCallback callback){
		AsyncHttpClient Client = new AsyncHttpClient();
		RequestParams request = new RequestParams();
		request.put("image", "png");
		String[] allowType = new String[]{"image/png; charset=utf-8"};
		Client.post(url, request,new BinaryHttpResponseHandler(allowType){
			@Override
			public void onSuccess(byte[] image){
				/*
				 * throughout this
				 *  to deal with image's bytes which is 
				 *  downloaded by such class
				 */
				
				Log.d("I_GET","loading pictuer "+image.toString());
				callback.callback(image);
			}
			@Override
			public void onFailure(int statusCode, Header[] headers, byte[] binaryData, Throwable error){
				Log.d("I_CACHE","error"+error.toString());
				for (Header head : headers){
					Log.d(TAG,head.getName() + "/"+head.getValue());
				}
			}
			
		});
		
	}
	
	interface DealByteCallback{
		public void callback(byte[] image);
	}
	
}
